package cn.tedu.map;

import org.junit.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/*本类用于Map测试
* map本身没有迭代器 需要转换成其他集合进行迭代
* 方案一 将map中的所有key 取出放入set集合
* 方案二  set<Entry<K,V>> entrySet() 把map中的key value取出来 形成一个个 entry
* 通过entry的 getKey()与getValue()分别获取 key和value*/
public class TestMap2 {
    @Test
public void test(){
        HashMap<String, Integer> hashMap = new HashMap<>();
        hashMap.put("张三",18);
        /*方式一 entrySet*/
        Set<Map.Entry<String, Integer>> entries = hashMap.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Integer> next = iterator.next();
            String key = next.getKey();
            Integer value = next.getValue();
            System.out.println("key="+key+"value="+value);
        }

        /*方式二 keySet*/
        Set<String> strings = hashMap.keySet();
        Iterator<String> iterator1 = strings.iterator();
        while (iterator1.hasNext()){
            String next = iterator1.next();
            Integer integer = hashMap.get(next);
            System.out.println("key="+next+"value="+integer);
        }
        System.gc();
    }
    @Test
    public void test1(){
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"张三");
        map.put(2,"李四");
        map.put(3,"王五");
        map.put(4,"唐僧");
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        Iterator<Map.Entry<Integer, String>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, String> next = iterator.next();
            Integer key = next.getKey();
            next.setValue("安好是个");
            String value = next.getValue();
            System.out.println("key="+key+"value="+value);

            }
        System.out.println("**************************");
        System.out.println(map);
        System.out.println("**********************");
        /*2.*/
        Set<Integer> integers = map.keySet();
        Iterator<Integer> iterator1 = integers.iterator();
        while (iterator1.hasNext()){
            Integer next1 = iterator1.next();
            String s = map.get(next1);
            System.out.println("key="+next1+"\tvalue="+s);
        }

    }
    @Test
    public void test2(){
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"张三");
        map.put(2,"李四");
        map.put(3,"王五");
        map.put(4,"唐僧");
        Set<Integer> set = map.keySet();
        Iterator<Integer> iterator = set.iterator();
        while (iterator.hasNext()){
            Integer next = iterator.next();
            String s = map.get(next);
            System.out.println("key="+next+"\t value="+s);
        }
        /*2.*/
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        Iterator<Map.Entry<Integer, String>> iterator1 = entries.iterator();
        while (iterator1.hasNext()){
            Map.Entry<Integer, String> next = iterator1.next();
            Integer key = next.getKey();
            String value = next.getValue();
            System.out.println("key="+key+"\t value="+value);

        }
    }
    @Test
    public void test3(){
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"张三");
        map.put(2,"李四");
        map.put(3,"王五");
        map.put(4,"唐僧");
        Set<Integer> integers = map.keySet();
        Iterator<Integer> iterator = integers.iterator();
        while (iterator.hasNext()){
            Integer next = iterator.next();
            String s = map.get(next);
            System.out.println(next+"\t"+s);
        }
        System.out.println("***********************************************************");
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        Iterator<Map.Entry<Integer, String>> iterator1 = entries.iterator();
        while (iterator1.hasNext()){
            Map.Entry<Integer, String> next = iterator1.next();
            System.out.println(next.getKey()+"\t"+next.getValue());
        }
    }
    @Test
    public void test4(){
        HashMap<Integer, String> map = new HashMap<>();
        map.put(1,"张三");
        map.put(2,"李四");
        map.put(3,"王五");
        map.put(4,"唐僧");
        map.put(5,"张三不李四");
        Set<Integer> integers = map.keySet();
        Iterator<Integer> iterator = integers.iterator();
        while (iterator.hasNext()){
            Integer next = iterator.next();
            System.out.println(next+"\t"+map.get(next));
        }
        System.out.println("================================================================");
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        Iterator<Map.Entry<Integer, String>> iterator1 = entries.iterator();

        while (iterator1.hasNext()){
            Map.Entry<Integer, String> next = iterator1.next();
            System.out.println(next.getKey()+"\t"+next.getValue());
        }
    }
}
